package com.andriology.ui;

import java.util.Locale;

import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.andriology.ommar_el_ard.R;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends SherlockFragmentActivity {

	private String ACTIVE_TAB_INDEX = "activeTab";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TabListener tabListner_1 = null;
        TabListener tabListner_2 = null;
        int index = -1;
        
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        
        /* TODO: Check this option out:
         * actionBar.setDisplayShowTitleEnabled(false);*/

        String lang = Locale.getDefault().getDisplayLanguage();
        
        /*if(!lang.equals("العربية"))
        {
        	 show dialog message to change device settings into Arabic language
        	ArabicAlertDialogFragment alertDialog = new ArabicAlertDialogFragment();
        	alertDialog.show(getSupportFragmentManager(), "alertDialogTag");
        	
        	return;
        }*/
       
    	tabListner_1 = new TabListener<AllVideos>(this, "videos_tag", AllVideos.class);
    	tabListner_2 = new TabListener<AllTestsActivity>(this, "tests_tag", AllTestsActivity.class);
    	index = 1;
        
        Tab tab = actionBar.newTab()
                .setText(R.string.tab1_title)
                .setTabListener(tabListner_1);
        actionBar.addTab(tab);

        tab = actionBar.newTab()
            .setText(R.string.tab2_title)
            .setTabListener(tabListner_2);
        actionBar.addTab(tab);
        
        if(savedInstanceState != null){
        	index = savedInstanceState.getInt(ACTIVE_TAB_INDEX);
        	
        	/* if index is still -1 while saved instance state is not null, this is indication for running the application for first time
        	 * with non-arabic language so just reset it to 1 in this special case.*/
        	if (index == -1)
        		index = 1;
        }
        
        actionBar.setSelectedNavigationItem(index);
        
    }


	@Override
	protected void onSaveInstanceState(Bundle outState) {
    	
    	outState.putInt(ACTIVE_TAB_INDEX, getSupportActionBar().getSelectedNavigationIndex());
		super.onSaveInstanceState(outState);
	}

	public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
        private Fragment mFragment;
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;

        /** Constructor used each time a new tab is created.
          * @param activity  The host Activity, used to instantiate the fragment
          * @param tag  The identifier tag for the fragment
          * @param clz  The fragment's Class, used to instantiate the fragment
          */
        public TabListener(Activity activity, String tag, Class<T> clz) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
        }

        /* The following are each of the ActionBar.TabListener callbacks */

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
        	
        	/*Check to see if we already have a fragment for this tab, probably
             from a previously saved state.  If so, use it directly i.e. attach it in order to show */
        	mFragment = ((FragmentActivity) mActivity).getSupportFragmentManager().findFragmentByTag(mTag);
        	
            /* Check if the fragment was already initialized before*/
            if ((mFragment == null) /*&& !mFragment.isDetached()*/) {
                // If not, instantiate and add it to the activity
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.add(android.R.id.content, mFragment, mTag);
            } else {
                // If it exists, simply attach it in order to show it
                ft.attach(mFragment);
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
                // Detach the fragment, because another one is being attached
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // User selected the already selected tab. Usually do nothing.
        }
    }
	
	public class ArabicAlertDialogFragment extends DialogFragment {
		
	    @Override
	    public Dialog onCreateDialog(Bundle savedInstanceState) {
	        // Use the Builder class for convenient dialog construction
	        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	        builder.setMessage(R.string.arabic_alert_msg)
	               .setPositiveButton(R.string.arabic_alert_ok, new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                       
	                	   /* Open settings menu */
	                	   if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB){
	                		   Intent intent = new Intent();
	                		   intent.setClassName("com.android.settings", "com.android.settings.LocalePicker");
	                		   startActivity(intent);
	                	   }
	                	   else {
	                		   startActivity(new Intent(android.provider.Settings.ACTION_LOCALE_SETTINGS));
	                	   }
	                	   
	                   }
	               })
	               .setNegativeButton(R.string.arabic_alert_no, new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                       /*User cancelled the dialog*/
	                   }
	               });
	        
	        // Create the AlertDialog object and return it
	        return builder.create();
	    }
	}
}
